#!/bin/sh
#
# Suss out what sort of platform I am ...
#
# whatami [-d][-h]
#

fmt="%-19.19s %-8.8s %-7.7s %s%s%s\n"
sts=0
debug=false
heading=false

tmp=/var/tmp/$$
trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15

while [ $# -gt 0 ]
do
    case "$1"
    in
	-d)
	    debug=true
	    ;;
	-h)
	    heading=true
	    ;;
	*)
	    echo >&2 "Usage: whatami [-d][-h]"
	    sts=1
	    exit
	    ;;
    esac
    shift
done

if $heading
then
    printf "$fmt" "Host" "PCP" "CPU" "Operating System and Options"
    printf "$fmt" "-------------------" "--------" "------" "-----------------------------"
fi

from=''
if which uname >/dev/null 2>&1
then
    arch=`uname -m`
    [ "$arch" = unknown ] && arch=`uname -p`
    os="`uname -sr`"
    $debug && from=' [uname -sr]'
else
    arch=unknown
    os=unknown
fi
if [ -f /etc/SuSE-release ]
then
    # SUSE Linux Enterprise Server 11 (ia64)
    os=`sed </etc/SuSE-release -n -e '1{
s/ (.*//
s/Linux Enterprise Server /SLES/
p
}'`
    os="$os`sed </etc/SuSE-release -n -e '/^PATCHLEVEL /{
s/.*=[ 	]*/ SP/
p
}'`"
    os="$os`sed </etc/SuSE-release -n -e '/^CODENAME /{
s/.*=[ 	]*/ (/
s/$/)/
p
}'`"
    $debug && from=' [/etc/SuSE-release]'
elif [ -f /etc/centos-release ]
then
    # CentOS release 6.5 (Final)
    os=`sed </etc/centos-release -n -e '1{
s/ release //
p
}'`
    $debug && from=' [/etc/centos-release]'
elif [ -f /etc/redhat-release ]
then
    # CentOS release 5.6 (Final)
    # Red Hat Enterprise Linux Server release 6.6 (Santiago)
    # Red Hat Enterprise Linux Server release 7.1 (Maipo)
    #
    os=`sed </etc/redhat-release \
	-e 's/ release / /' \
	-e 's/Red Hat /RH/' \
	-e 's/Enterprise Linux /EL /'`
    $debug && from=' [/etc/redhat-release]'
elif [ -f /etc/lsb-release ]
then
    # Debian-based distros tend to use this way
    # DISTRIB_ID=LinuxMint
    # DISTRIB_RELEASE=12
    # DISTRIB_CODENAME=lisa
    # or
    # DISTRIB_CODENAME="Libretto"
    #
    id=`sed </etc/lsb-release -n -e '/^DISTRIB_ID *= */s///p'`
    release=`sed </etc/lsb-release -n -e '/^DISTRIB_RELEASE *= */s///p'`
    codename=`sed </etc/lsb-release -n -e 's/"//g' -e '/^DISTRIB_CODENAME *= */s///p'`
    os="$id $release ($codename)"
    $debug && from=' [/etc/lsb-release]'
elif [ -f /etc/debian_version ]
then
    # contains just the version number, e.g.
    # 11.1
    #
    os="Debian `cat /etc/debian_version`"
    if [ -f /etc/os-release ]
    then
	# VERSION_CODENAME=bullseye
	#
	codename=`sed </etc/os-release -n -e '/^VERSION_CODENAME= */{
s///
s/"//g
p
}'`
	[ -n "$codename" ] && os="$os ($codename)"
    fi
    $debug && from=' [/etc/debian_version]'
elif [ -f /etc/mandriva-release ]
then
    # Mandriva Linux release 2011.0 (Official) for x86_64
    #
    os=`sed </etc/mandriva-release -e 's/ release / /' -e 's/ (Official)/ /' -e 's/ for .*//'`
    $debug && from=' [/etc/mandriva-release]'
elif [ -f /etc/gentoo-release ]
then
    # Gentoo Base System release 2.0.3
    #
    os=`sed </etc/gentoo-release -e 's/ release / /' -e 's/ Base System / /'`
    $debug && from=' [/etc/gentoo-release]'
elif [ -f /etc/system-release ]
then
    # Fedora release 15 (Lovelock)
    #
    os=`sed </etc/system-release -e 's/ release / /'`
    $debug && from=' [/etc/system-release]'
elif [ -f /etc/os-release ]
then
    # NAME=openSUSE
    # VERSION = 12.1 (Asparagus)
    # or
    # NAME="Arch Linux"
    # or
    # NAME=FreeBSD
    # VERSION="13.2-RELEASE-p5"
    # or
    # NAME="openSUSE Leap"
    # VERSION="15.6 Beta"
    #
    name=`sed </etc/os-release -n -e '/^NAME *= */{
s///
s/"//g
p
}'`
    name=`echo "$name" | sed -e 's/"//g'`
    if [ "$name" = FreeBSD ]
    then
	sed </etc/os-release -e '/^VERSION *=/s/-RELEASE-p[0-9][0-9]*//'
    elif [ "$name" = 'openSUSE Leap' ]
    then
	sed </etc/os-release -e '/^VERSION *=/s/ Beta//'
    else
	cat /etc/os-release
    fi >$tmp.out
    ver=`sed <$tmp.out -n -e '/^VERSION *= */{
s/^VERSION *= *//
s/"//g
p
}'`
    os="$name $ver"
    $debug && from=' [/etc/os-release]'
elif [ -f /etc/release ]
then
    #    OpenSolaris 2010.05 snv_134b X86
    #
    #    OpenIndiana Development oi_148 X86
    #
    #    OpenIndiana Hipster 2017.10 (powered by illumos)
    #    ... and more lines ...
    #
    try=`sed </etc/release -n -e '/Open[A-Z]/{
s/.*Open/Open/
s/ /~/
s/ /~/
s/ .*//
s/~/ /g
p
q
}'`
    if [ -n "$try" ]
    then
	os="$try"
	$debug && from=' [/etc/release]'
    fi
elif [ -f /etc/slackware-version ]
then
    os=`cat /etc/slackware-version`
fi
# pmcd.version 1 "3.6.2"
#
if which pmprobe >/dev/null 2>&1
then
    if pmprobe -v pmcd.version >$tmp.out 2>/dev/null
    then
	pcpver=`sed <$tmp.out -e 's/"$//' -e 's/.*"//'`
    fi
    [ -z "$pcpver" ] && pcpver=unknown
else
    pcpver=unknown
fi

opts=''

# (Linux) kernel version
#
#
if which pmprobe >/dev/null 2>&1
then
    if pmprobe -v kernel.uname.release >$tmp.out 2>/dev/null
    then
	# variants here include ...
	# kernel.uname.release 1 "..."
	# Linux
	#	5.4.0-121-generic
	#	5.17.11-100.fc34.x86_64
	#	4.18.0-394.el8.x86_64
	#	5.14.14-arch1-1
	# OpenBSD
	#	7.0
	#
	kernel=`sed <$tmp.out -e 's/"$//' -e 's/.*"//' -e 's/-.*//'`
    fi
    [ -z "$kernel" ] && kernel=unknown
else
    kernel=unknown
fi
opts="$opts kernel=$kernel"

# Python version
#
if which pmpython >/dev/null 2>&1
then
    if pmpython --version >$tmp.out 2>&1
    then
	opts="$opts `sed -e 's/Python /py=/' <$tmp.out`"
    else
	opts="$opts py=none"
    fi
else
    opts="$opts py=none"
fi

# SELinux?
#
if which getenforce >/dev/null 2>&1
then
    opts="$opts se=`getenforce | tr '[A-Z]' '[a-z]'`"
else
    opts="$opts se=none"
fi
opts=" [`echo $opts | sed -e 's/^ //'`]"

printf "$fmt" "`hostname | sed -e 's/\..*//'`" "$pcpver" "$arch" "$os" "$from" "$opts"

exit 0
